var PAGEURLARRAY = window.location.href.toString().split(window.location.host)[1].substring(1).split('=')[0].split('?');
var CONTROLLER = PAGEURLARRAY[0].split('/')[PAGEURLARRAY[0].split('/').length - 1] || 'index';
var ACTION = PAGEURLARRAY[1] || 'index';

if (!Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    };
}

function escapePost(str) {
    if (str) {
        str += '';
        str = str.replace(/%/gm, '%25');
        str = str.replace(/\+/gm, '%2B');
        str = str.replace(/&/gm, '%26');
        str = str.replace(/=/gm, '%3D');
        str = str.replace(/ /gm, '%20');
        str = str.replace(/\t/gm, '%09');
        str = str.replace(/\r\n/gm, '%0D');
        str = str.replace(/\r/gm, '%0D');
        str = str.replace(/\n/gm, '%0D');
    }
    return str;
}

function parseURL(url)
{
    var a = document.createElement('a');
    a.href = url + window.location;
    return a;
}

//if (!Array.indexOf) {
//    Array.prototype.indexOf = function(obj) {
//        for (var i = 0; i < this.length; i++) {
//            if (this[i] == obj) {
//                return i;
//            }
//        }
//        return -1;
//    };
//}

function setParamsToCurrentUrl(params)
{
    var url = '' + parseURL('').href;
    var urlParams = {};
    if (!params) {
        params = {};
    }

    if (url.indexOf('?') !== -1) {
        var tmpParams = url.split('?', 2)[1].split('&');
        url = url.split('?', 2)[0];
        for (var i in tmpParams) {
            if (typeof tmpParams[i] === 'string') {
                var tmpParam = tmpParams[i].split('=', 2);
                if (tmpParam[0] !== '') {
                    urlParams['' + tmpParam[0]] = '' + (tmpParam[1] === undefined ? '' : tmpParam[1]);
                }
            }
        }
    }

    for (var i in params) {
        urlParams[i] = params[i];
    }

    for (var i in urlParams) {
        url += (url.indexOf('?') !== -1 ? '&' : '?') + escapePost(i) + '=' + escapePost(urlParams[i]);
    }

    return url;
}
